package com.test.baseadapter;

import com.test.baseadapter.BaseAdapterTestActivity.TestAdapter.ViewHolder;

import android.app.ListActivity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SoundEffectConstants;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class BaseAdapterTestActivity extends ListActivity {
	
	private static final String LOG_TAG = "BaseAdapterTestActivity";
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        ListAdapter adapter = new TestAdapter(getApplicationContext());
        setListAdapter(adapter);
    }
    
    @Override
    protected void onListItemClick(ListView listview, View view, int position, long id) {
    	// TODO Auto-generated method stub
//    	ViewHolder holder = (ViewHolder)view.getTag();
//    	if (holder.favorite.isSoundEffectsEnabled()) {
//    		holder.favorite.playSoundEffect(SoundEffectConstants.CLICK);
//		}
    	super.onListItemClick(listview, view, position, id);
    }
    
    protected class TestAdapter extends BaseAdapter {
    	
    	protected LayoutInflater mInflater;
    	protected Bitmap mIcon;
    	
    	protected TestAdapter(Context context) {
    		mInflater = LayoutInflater.from(context);
    		mIcon = BitmapFactory.decodeResource(context.getResources(), R.drawable.jesus_icon);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return DATA.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder;
			
            if (convertView == null) {
				convertView = mInflater.inflate(R.layout.list_item, null);
				
				holder = new ViewHolder();
				holder.icon = (ImageView)convertView.findViewById(R.id.category_icon_img);
				holder.text = (TextView)convertView.findViewById(R.id.title);
				holder.favorite = (CheckBox)convertView.findViewById(R.id.favorite_checkbox);
				holder.select = (CheckBox)convertView.findViewById(R.id.select_checkbox);
				
				convertView.setTag(holder);
			}
            else {
            	holder = (ViewHolder) convertView.getTag();
			}
            
            holder.icon.setImageBitmap(mIcon);
            holder.text.setText(DATA[position]);
            holder.favorite.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View view) {
					// TODO Auto-generated method stub
				}
			});
            
			return convertView;
		}
    	
		protected class ViewHolder {
			ImageView icon;
            TextView text;
            CheckBox favorite;
            CheckBox select;
        }
    }
    
    public static final String[] DATA = {"1", "2", "3", "4", "5"};
}